package com.qkjia.srb.sms.controller.api;

import com.qkjia.common.exception.Assert;
import com.qkjia.common.result.R;
import com.qkjia.common.result.ResponseEnum;
import com.qkjia.common.utils.RandomUtils;
import com.qkjia.common.utils.RegexValidateUtils;
import com.qkjia.srb.sms.client.CoreUserInfoClient;
import com.qkjia.srb.sms.service.SmsService;
import com.qkjia.srb.sms.util.SmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *  @author 赵乾坤
 *  @since 2021-11-07 20:32
 */
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
//@CrossOrigin//跨域
@Slf4j
public class ApiSmsController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SmsService smsService;

    @Resource
    private CoreUserInfoClient coreUserInfoClient;

    @ApiOperation("获取短信验证码")
    @GetMapping("/send/{mobile}")
    public R sendSMS(@ApiParam(value = "手机号", required = true)
                  @PathVariable String mobile) {

        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);


        // 判断手机号有没有注册
        boolean result = coreUserInfoClient.checkMobile(mobile);
        log.info("result = " + result);
        Assert.isTrue(result==false,ResponseEnum.MOBILE_EXIST_ERROR);

        //生成验证码
        String code = RandomUtils.getFourBitRandom();
        //组装短信模板参数
        HashMap<String, Object> param = new HashMap<>();
        param.put("code", code);
        //发送短信
        // 测试的时候可以不发短信 在redis校验
//        smsService.send(mobile, SmsProperties.TEMPLATE_CODE, param);
        //将验证码存入redis
        redisTemplate.opsForValue().set("srb:sms:code:" + mobile,code,5,TimeUnit.MINUTES);
    return R.ok().message("短信发送成功");

    }

    @ApiOperation("获取邮箱验证码")
    @GetMapping("/sendMail/{addressee}")
    public R sendMail( @ApiParam(value = "收件人邮箱号", required = true)
                   @PathVariable String addressee){
        log.info("邮件发送");
        //            Email_NULL_ERROR(-202, "邮箱号不能为空"),
        Assert.notEmpty(addressee,ResponseEnum.Email_NULL_ERROR);
        //     Email_ERROR(-203, "邮箱格式不正确"),
//        Assert.isTrue(RegexValidateUtils.checkEmail(addressee),ResponseEnum.Email_ERROR);

        /**
         * 远程调用service-core微服务接口校验手机号是否注册
         */
//        boolean b = coreUserInfoClient.checkMobile(addressee);
//        Assert.isTrue(b == false,ResponseEnum.MOBILE_EXIST_ERROR);

        Map<String,Object> param = new HashMap<>();
        //获取六位数验证码
        String code = RandomUtils.getFourBitRandom();
        param.put("code",code);
        smsService.sendMail(addressee,param);
        redisTemplate.opsForValue().set("srb:mail:code:" + addressee,code,5, TimeUnit.MINUTES );
        return R.ok().message("验证码发送成功");

    }

}
